package br.com.felix.clearcase.command;

import br.com.felix.easyclearcase.EasyClearCaseFile;
import br.com.felix.easyclearcase.EasyClearCaseFile.ClearCaseStatus;

/**
 * This class encapsulates the cleartool command to checkin a file/folder that is checked out.
 * File must be checked out.
 * @see ClearToolCommand
 * */
public class Checkin extends ClearToolCommand{

	private String comment;
	private EasyClearCaseFile newVersionFile;
	private boolean fromRepository = false;
	
	
	/**
	 * Instantiates a new Checkin command that will copy a local file into the vob. The correspondent file in the vob must be checked out.
	 * @param newVersionFile the latest version of the file that you want to commit that is in the local filesystem (ie not in the vob).
	 * @param repositoryFile the file that you want to update with the latest version.
	 * @param comment the comment to be registered with clearcase.
	 * */
	public Checkin(EasyClearCaseFile newVersionFile, EasyClearCaseFile repositoryFile, String comment) {
		this.newVersionFile = newVersionFile;
		this.file = repositoryFile;
		this.comment = comment;
	}
	
	/**
	 * Instantiates a new Checkin command that will only checkin a checkouted file in the vob.
	 * @param newVersionFile the latest version of the file that you want to commit that is in the local filesystem (ie not in the vob).
	 * @param repositoryFile the file that you want to update with the latest version.
	 * @param comment the comment to be registered with clearcase.
	 * */
	public Checkin(EasyClearCaseFile repositoryFile, String comment) {
		this.file = repositoryFile;
		this.comment = comment;
	}

	
	/**
	 * @see ClearToolCommand#toCommandLine()
	 * */
	@Override
	public String toCommandLine() {
		fromRepository = (newVersionFile != null && file != null);
		if(fromRepository)
			return "cleartool checkin -rm -comment \""+comment+"\" -from "+newVersionFile.getAbsolutePath()  +" "+file.getAbsolutePath();
		else
			return "cleartool checkin -rm -comment \""+comment+"\" \""+file.getAbsolutePath()+"\"";
	}

	@Override
	public String getCommandName() {
		return "Checkin";
	}

	@Override
	public ClearCaseStatus getStatusAfterExecution() {
		return ClearCaseStatus.FILE_ELEMENT;
	}
	
}
